package org.csu.mypetstore.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Item;
import org.csu.mypetstore.api.entity.ItemInventory;
import org.csu.mypetstore.api.entity.LongCart;
import org.csu.mypetstore.api.entity.Product;
import org.csu.mypetstore.api.persistence.ItemInventoryMapper;
import org.csu.mypetstore.api.persistence.ItemMapper;
import org.csu.mypetstore.api.persistence.LongCartMapper;
import org.csu.mypetstore.api.persistence.ProductMapper;
import org.csu.mypetstore.api.service.LongCartService;
import org.csu.mypetstore.api.vo.AccountVO;
import org.csu.mypetstore.api.vo.ItemVO;
import org.csu.mypetstore.api.vo.LongCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.NumberUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service("longCartService")
public class LongCartServiceImpl implements LongCartService {

    @Autowired
    private LongCartMapper longCartMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemInventoryMapper itemInventoryMapper;

    private ItemVO itemToItemVO(Item item,Product product,LongCart longCart){
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
        itemVO.setAttribute4(item.getAttribute4());
        itemVO.setAttribute5(item.getAttribute5());

        itemVO.setCategoryId(product.getCategory());
        itemVO.setProductName(product.getProductName());
        itemVO.setProductDescription(product.getProductDescription());
//        ItemInventory itemInventory = itemInventoryMapper.selectById(item.getItemId());
//        itemVO.setQuantiy(itemInventory.getQuantiy());


        itemVO.setQuantiy(longCart.getQuantity());

        return itemVO;
    }

    public LongCartVO toLongCartVO(List<LongCart> longCartList){
        LongCartVO longCartVO = new LongCartVO();
        List<ItemVO>  itemVOList = new ArrayList<>();
        for(LongCart longCartt :longCartList){
            Item item = itemMapper.selectById(longCartt.getItemID());
            Product product = productMapper.selectById(item.getProductId());
            ItemVO itemVO = itemToItemVO(item,product,longCartt);
            itemVOList.add(itemVO);
            BigDecimal q = new BigDecimal(String.valueOf(longCartt.getQuantity()));
            if(longCartVO.getCart_price()==null){
                longCartVO.setCart_price(new BigDecimal(0));
            }
            longCartVO.setCart_price(longCartVO.getCart_price().add(q.multiply(item.getListPrice())));

        }
        longCartVO.setItemVOList(itemVOList);
        return longCartVO;

    }

    @Override
    public CommonResponse <LongCartVO> getLongCart(String username) {
        QueryWrapper<LongCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<LongCart> longCartList = longCartMapper.selectList(queryWrapper);
        if(longCartList.isEmpty()){
            return CommonResponse.createForSuccessMessage("购物车为空");
        }
        LongCartVO longCartVO = toLongCartVO(longCartList);
        return CommonResponse.createForSuccess(longCartVO);
    }

    private ItemVO itemandotherToItemVO(Item item,Product product){
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
        itemVO.setAttribute4(item.getAttribute4());
        itemVO.setAttribute5(item.getAttribute5());
        itemVO.setCategoryId(product.getCategory());
        itemVO.setProductName(product.getProductName());
        itemVO.setProductDescription(product.getProductDescription());

        ItemInventory itemInventory = itemInventoryMapper.selectById(item.getItemId());
        itemVO.setQuantiy(itemInventory.getQuantiy());

        return itemVO;
    }

    @Override
    public CommonResponse<ItemVO> getProductInLongCart(String ItemId) {
        Item item = itemMapper.selectById(ItemId);
        Product product = productMapper.selectById(item.getProductId());
        if(item==null){
            return CommonResponse.createForSuccessMessage("购物车中没有该商品");
        }
        ItemVO itemVO = itemandotherToItemVO(item,product);
        return CommonResponse.createForSuccess(itemVO);
    }

    @Override
    public CommonResponse <LongCartVO> insertItemtoLongCart(String username, String itemId) {
        QueryWrapper<LongCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("itemId",itemId);
        List<LongCart> longCartList = longCartMapper.selectList(queryWrapper);
        System.out.println(longCartList.size());
        if(longCartList.size() != 0){
            for(LongCart longCart :longCartList){
                if(longCart==null){
                    return CommonResponse.createForSuccessMessage("添加购物车失败");
                }
                longCart.setQuantity(longCart.getQuantity()+1);
                longCartMapper.updateById(longCart);
            }
        }
        else  {
            System.out.println("购物车中没有该商品，新添加条目");
            LongCart longCart = new LongCart();
            longCart.setUsername(username);
            longCart.setItemID(itemId);
            longCart.setQuantity(1);
            longCartMapper.insert(longCart);
            System.out.println(longCart);
            if(longCart==null){
                return CommonResponse.createForSuccessMessage("添加购物车失败");
            }
        }
        return getLongCart(username);
    }

    @Override
    public CommonResponse <LongCartVO> deleteItemInLongCart(String username, String itemId) {
        QueryWrapper<LongCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("itemId",itemId);
        List<LongCart> longCartList = longCartMapper.selectList(queryWrapper);
        if(longCartList.size()==0){
            return CommonResponse.createForSuccessMessage("该商品不存在");
        }
        for(LongCart longCart : longCartList){
            if(longCart.getQuantity()<=1){
                longCartMapper.delete(queryWrapper);
            }
            else {
                longCart.setQuantity(longCart.getQuantity()-1);
                longCartMapper.updateById(longCart);
            }
        }
        //longCartMapper.delete(queryWrapper);
        return getLongCart(username);
    }
}
